package com.jjb.saas.demo.enums.role;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.jjb.saas.framework.enums.annotation.EnumInfoAnnotation;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author tzh * @date 2023年02月15日 11:10
 */
@AllArgsConstructor
@EnumInfoAnnotation(chineseName = "角色所属编码", dictType = "sys_role_type_enum")
public enum RoleTypeEnum {
    SUPER_ADMIN("SUPER_ADMIN", "超级管理员"),
    TENANT_ADMIN("TENANT_ADMIN", "租户管理员"),
    CHANNEL_ADMIN("CHANNEL_ADMIN", "渠道管理员"),
    ORG_ADMIN("ORG_ADMIN", "机构管理员"),
    CAMPUS_ADMIN("CAMPUS_ADMIN", "校区管理员"),
    STUDENT("STUDENT", "学员"),
    TEACHER("TEACHER", "教师"),
    EXAM_ADMIN("EXAM_ADMIN", "注册考试中心管理员"),
    EXPERT("EXPERT", "专家"),
    FINANCE_ADMIN("FINANCE_ADMIN", "财务管理员"),
    GOV_ADMIN("GOV_ADMIN", "政府端管理员");
    @Getter
    @JsonValue
    @EnumValue
    private final String value;
    @Getter
    private final String label;



    @JsonCreator
    public static RoleTypeEnum fromValue(Object flag) {

        if (null == flag) {
            return null;
        }
        String key = null;
        if (flag instanceof Map) {
            key = ((Map<String, String>) flag).get("code");
        } else if (flag instanceof String) {
            key = (String) flag;
        }
        String finalKey = key;
        return Arrays.stream(RoleTypeEnum.values())
                .filter(anEnum -> anEnum.getValue().equals(finalKey))
                .findFirst().orElse(null);
    }

    private static final Map<String, RoleTypeEnum> VALUES = new HashMap<>();

    static {
        for (final RoleTypeEnum e : RoleTypeEnum.values()) {
            RoleTypeEnum.VALUES.put(e.getValue(), e);
        }
    }


    public RoleTypeEnum of(String code) {
        return VALUES.get(code);
    }
}
